package org.wiztools.te;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

/**
Class for parsing the key mapping.
*/

public class KeyBinding{
	private String[] alphabet;
	private int[] alphabetKey;
	
	private String[] number;
	private int[] numberKey;
	
	private String[] function;
	private int[] functionKey;
	
	public KeyBinding(){
		String[] alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
		int[] alphabetKey = {KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_E, KeyEvent.VK_F, KeyEvent.VK_G, KeyEvent.VK_H, KeyEvent.VK_I, KeyEvent.VK_J, KeyEvent.VK_K, KeyEvent.VK_L, KeyEvent.VK_M, KeyEvent.VK_N, KeyEvent.VK_O, KeyEvent.VK_P, KeyEvent.VK_Q, KeyEvent.VK_R, KeyEvent.VK_S, KeyEvent.VK_T, KeyEvent.VK_U, KeyEvent.VK_V, KeyEvent.VK_W, KeyEvent.VK_X, KeyEvent.VK_Y, KeyEvent.VK_Z};
		this.alphabet = alphabet;
		this.alphabetKey = alphabetKey;
		
		String[] number = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
		int[] numberKey = {KeyEvent.VK_0, KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_3, KeyEvent.VK_4, KeyEvent.VK_5, KeyEvent.VK_6, KeyEvent.VK_7, KeyEvent.VK_8, KeyEvent.VK_9};
		this.number = number;
		this.numberKey = numberKey;
		
		String[] function = {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12"};
		int[] functionKey = {KeyEvent.VK_F1, KeyEvent.VK_F2, KeyEvent.VK_F3, KeyEvent.VK_F4, KeyEvent.VK_F5, KeyEvent.VK_F6, KeyEvent.VK_F7, KeyEvent.VK_F8, KeyEvent.VK_F9, KeyEvent.VK_F10, KeyEvent.VK_F11, KeyEvent.VK_F12};
		this.function = function;
		this.functionKey = functionKey;
	}
	
	
	/**
	Given a key name, returns the code.
	*/
	public int getKeyCode(String str){
		int i = 0;
		if(str==null){
			return -1;
		}
		if((i=processKey(alphabet, alphabetKey, str))!=-1){
			return i;
		}
		if((i=processKey(number, numberKey, str))!=-1){
			return i;
		}
		if((i=processKey(function, functionKey, str))!=-1){
			return i;
		}
		return i;
	}
	
	
	/**
	Returns the masks's code.
	*/
	public int getKeyMask(String mask){
		int m = 0;
		if(mask==null){
			return m;
		}
		if(mask.equals("SHIFT")){
			m = ActionEvent.SHIFT_MASK;
		}
		else if(mask.equals("CTRL")){
			m = ActionEvent.CTRL_MASK;
		}
		else if(mask.equals("ALT")){
			m = ActionEvent.ALT_MASK;
		}
		return m;
	}
	
	private int processKey(String[] strarr, int[] arr, String str){
		for(int i=0;i<strarr.length;i++){
			if(str.equals(strarr[i])){
				return arr[i];
			}
		}
		return -1;
	}
}
